home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / util / arcer / gnutar10.lha / GnuTAR / GNUTarSource.LHA / source / list.c < prev    next >
C/C++ Source or Header  |  1991-07-04  |  16KB  |  710 lines

  1. /* List a tar archive.
  2.    Copyright (C) 1988 Free Software Foundation
  3.  
  4. This file is part of GNU Tar.
  5.  
  6. GNU Tar is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Tar is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Tar; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * List a tar archive.
  22.  *
  23.  * Also includes support routines for reading a tar archive.
  24.  *
  25.  * this version written 26 Aug 1985 by John Gilmore (ihnp4!hoptoad!gnu).
  26.  *
  27.  * @(#)list.c 1.31 11/5/87 - gnu
  28.  */
  29. #include <stdio.h>
  30. #include <ctype.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #ifndef MSDOS
  34. #include <sys/file.h>
  35. #endif    /* MSDOS */
  36.  
  37. #ifdef USG
  38. #include <sys/sysmacros.h>    /* major() and minor() defined here */
  39. #endif
  40.  
  41. #ifndef AMIGA
  42. char *ctime();                          /* From libc.a */
  43. #endif
  44.  
  45. #define isodigit(c)     ( ((c) >= '0') && ((c) <= '7') )
  46.  
  47. #include "tar.h"
  48. #include "port.h"
  49.  
  50. extern FILE *msg_file;
  51.  
  52. long from_oct();                        /* Decode octal number */
  53. void demode();                          /* Print file mode */
  54.  
  55. union record *head;            /* Points to current archive header */
  56. struct stat hstat;            /* Stat struct corresponding */
  57. int head_standard;            /* Tape header is in ANSI format */
  58.  
  59. void print_header();
  60. void skip_file();
  61. void skip_extended_headers();
  62.  
  63. extern char *quote_copy_string();
  64.  
  65.  
  66. /*
  67.  * Main loop for reading an archive.
  68.  */
  69. void
  70. read_and(do_something)
  71.     void (*do_something)();
  72. {
  73.     int status = 3;         /* Initial status at start of archive */
  74.     int prev_status;
  75.     extern time_t new_time;
  76.     char save_linkflag;
  77.  
  78.     name_gather();                  /* Gather all the names */
  79.     open_archive(1);                /* Open for reading */
  80.  
  81.     for(;;) {
  82.         prev_status = status;
  83.         status = read_header();
  84.         switch (status) {
  85.  
  86.         case 1:         /* Valid header */
  87.             /* We should decode next field (mode) first... */
  88.             /* Ensure incoming names are null terminated. */
  89.             head->header.name[NAMSIZ-1] = '\0';
  90.  
  91.             if (   !name_match(head->header.name)
  92.                 || (f_new_files && hstat.st_mtime<new_time)
  93.                 || (f_exclude && check_exclude(head->header.name))) {
  94.  
  95.                 int isextended = 0;
  96.  
  97.                 /* Skip past it in the archive */
  98.                 if (head->header.isextended)
  99.                     isextended = 1;
  100.                 save_linkflag = head->header.linkflag;
  101.                 userec(head);
  102.                 if (isextended) {
  103. /*                    register union record *exhdr;
  104.  
  105.                     for (;;) {
  106.                         exhdr = findrec();
  107.                         if (!exhdr->ext_hdr.isextended) {
  108.                         userec(exhdr);
  109.                         break;
  110.                         }
  111.                     }
  112.                     userec(exhdr);*/
  113.                     skip_extended_headers();
  114.                 }
  115.                 /* Skip to the next header on the archive */
  116.                 if(save_linkflag != LF_DIR)
  117.                     skip_file((long)hstat.st_size);
  118.                 continue;
  119.  
  120.             }
  121.  
  122.             (*do_something)();
  123.             continue;
  124.  
  125.             /*
  126.              * If the previous header was good, tell them
  127.              * that we are skipping bad ones.
  128.              */
  129.         case 0:         /* Invalid header */
  130.             userec(head);
  131.             switch (prev_status) {
  132.             case 3:     /* Error on first record */
  133.                 msg("Hmm, this doesn't look like a tar archive.");
  134.                 /* FALL THRU */
  135.             case 2:     /* Error after record of zeroes */
  136.             case 1:     /* Error after header rec */
  137.                 msg("Skipping to next file header...\n");
  138.             case 0:     /* Error after error */
  139.                 break;
  140.             }
  141.             continue;
  142.  
  143.         case 2:         /* Record of zeroes */
  144.             userec(head);
  145.             status = prev_status;    /* If error after 0's */
  146.             if (f_ignorez)
  147.                 continue;
  148.             /* FALL THRU */
  149.         case EOF:        /* End of archive */
  150.             break;
  151.         }
  152.         break;
  153.     };
  154.  
  155.     close_archive();
  156.     names_notfound();               /* Print names not found */
  157. }
  158.  
  159.  
  160. /*
  161.  * Print a header record, based on tar options.
  162.  */
  163. void
  164. list_archive()
  165. {
  166.     extern char *save_name;
  167.     int    isextended = 0; /* Flag to remember if head is extended */
  168.  
  169.     /* Save the record */
  170.     saverec(&head);
  171.  
  172.     /* Print the header record */
  173.     if (f_verbose) {
  174.         if (f_verbose > 1)
  175.             decode_header(head, &hstat, &head_standard, 0);
  176.         print_header();
  177.     }
  178.  
  179.     if(f_gnudump && head->header.linkflag==LF_DUMPDIR) {
  180.         size_t    size, written, check;
  181.         char    *data;
  182.         extern int errno;
  183.         extern long save_totsize;
  184.         extern long save_sizeleft;
  185.  
  186.         userec(head);
  187.         if(f_multivol) {
  188.             save_name = head->header.name;
  189.             save_totsize=hstat.st_size;
  190.         }
  191.         for(size = hstat.st_size;size>0;size-=written) {
  192.             if(f_multivol)
  193.                 save_sizeleft=size;
  194.             data = findrec()->charptr;
  195.             if(data==NULL) {
  196.                 msg("EOF in archive file?");
  197.                 break;
  198.             }
  199.             written = endofrecs()->charptr - data;
  200.             if(written>size)
  201.                 written=size;
  202.             errno=0;
  203.             check=fwrite(data,sizeof(char), written, msg_file);
  204.             userec((union record *)(data+written - 1));
  205.             if(check!=written) {
  206.                 msg_perror("only wrote %ld of %ld bytes to file %s",check, written,head->header.name);
  207.                 skip_file((long)(size)-written);
  208.                 break;
  209.             }
  210.         }
  211.         if(f_multivol)
  212.             save_name = 0;
  213.         saverec((union record **) 0);   /* Unsave it */
  214.         fputc('\n',msg_file);
  215.         fflush(msg_file);
  216.         return;
  217.  
  218.     }
  219.     saverec((union record **) 0);   /* Unsave it */
  220.     /* Check to see if we have an extended header to skip over also */
  221.     if (head->header.isextended)
  222.         isextended = 1;
  223.  
  224.     /* Skip past the header in the archive */
  225.     userec(head);
  226.  
  227.     /*
  228.      * If we needed to skip any extended headers, do so now, by
  229.      * reading extended headers and skipping past them in the
  230.      * archive.
  231.      */
  232.     if (isextended) {
  233. /*        register union record *exhdr;
  234.  
  235.         for (;;) {
  236.             exhdr = findrec();
  237.  
  238.             if (!exhdr->ext_hdr.isextended) {
  239.                 userec(exhdr);
  240.                 break;
  241.             }
  242.             userec(exhdr);
  243.         }*/
  244.         skip_extended_headers();
  245.     }
  246.  
  247.     if(f_multivol)
  248.         save_name=head->header.name;
  249.     /* Skip to the next header on the archive */
  250.  
  251.     skip_file((long) hstat.st_size);
  252.  
  253.     if(f_multivol)
  254.         save_name = 0;
  255. }
  256.  
  257.  
  258. /*
  259.  * Read a record that's supposed to be a header record.
  260.  * Return its address in "head", and if it is good, the file's
  261.  * size in hstat.st_size.
  262.  *
  263.  * Return 1 for success, 0 if the checksum is bad, EOF on eof,
  264.  * 2 for a record full of zeros (EOF marker).
  265.  *
  266.  * You must always userec(head) to skip past the header which this
  267.  * routine reads.
  268.  */
  269. int
  270. read_header()
  271. {
  272.     register int    i;
  273.     register long    sum, recsum;
  274.     register char    *p;
  275.     register union record *header;
  276.     long    from_oct();
  277.  
  278.     header = findrec();
  279.     head = header;        /* This is our current header */
  280.     if (NULL == header)
  281.         return EOF;
  282.  
  283.     recsum = from_oct(8,  header->header.chksum);
  284.  
  285.     sum = 0;
  286.     p = header->charptr;
  287.     for (i = sizeof(*header); --i >= 0;) {
  288.         /*
  289.          * We can't use unsigned char here because of old compilers,
  290.          * e.g. V7.
  291.          */
  292.         sum += 0xFF & *p++;
  293.     }
  294.  
  295.     /* Adjust checksum to count the "chksum" field as blanks. */
  296.     for (i = sizeof(header->header.chksum); --i >= 0;)
  297.         sum -= 0xFF & header->header.chksum[i];
  298.     sum += ' '* sizeof header->header.chksum;
  299.  
  300.     if (sum == recsum) {
  301.         /*
  302.          * Good record.  Decode file size and return.
  303.          */
  304.         if (header->header.linkflag == LF_LINK)
  305.             hstat.st_size = 0;    /* Links 0 size on tape */
  306.         else
  307.             hstat.st_size = from_oct(1+12, header->header.size);
  308.         return 1;
  309.     }
  310.  
  311.     if (sum == 8*' ') {
  312.         /*
  313.          * This is a zeroed record...whole record is 0's except
  314.          * for the 8 blanks we faked for the checksum field.
  315.          */
  316.         return 2;
  317.     }
  318.  
  319.     return 0;
  320. }
  321.  
  322.  
  323. /*
  324.  * Decode things from a file header record into a "struct stat".
  325.  * Also set "*stdp" to !=0 or ==0 depending whether header record is "Unix
  326.  * Standard" tar format or regular old tar format.
  327.  *
  328.  * read_header() has already decoded the checksum and length, so we don't.
  329.  *
  330.  * If wantug != 0, we want the uid/group info decoded from Unix Standard
  331.  * tapes (for extraction).  If == 0, we are just printing anyway, so save time.
  332.  *
  333.  * decode_header should NOT be called twice for the same record, since the
  334.  * two calls might use different "wantug" values and thus might end up with
  335.  * different uid/gid for the two calls.  If anybody wants the uid/gid they
  336.  * should decode it first, and other callers should decode it without uid/gid
  337.  * before calling a routine, e.g. print_header, that assumes decoded data.
  338.  */
  339. decode_header(header, st, stdp, wantug)
  340.     register union record    *header;
  341.     register struct stat    *st;
  342.     int    *stdp;
  343.     int    wantug;
  344. {
  345.  
  346.     long from_oct();
  347.  
  348.     st->st_mode = from_oct(8,  header->header.mode);
  349.     st->st_mtime = from_oct(1+12, header->header.mtime);
  350.     if(f_gnudump) {
  351.         st->st_atime = from_oct(1+12, header->header.atime);
  352.         st->st_ctime = from_oct(1+12, header->header.ctime);
  353.     }
  354.  
  355.     if (0==strcmp(header->header.magic, TMAGIC)) {
  356.         /* Unix Standard tar archive */
  357.         *stdp = 1;
  358.         if (wantug) {
  359. #ifdef NONAMES
  360.             st->st_uid = from_oct(8,  header->header.uid);
  361.             st->st_gid = from_oct(8,  header->header.gid);
  362. #else
  363.             st->st_uid = finduid(header->header.uname);
  364.             st->st_gid = findgid(header->header.gname);
  365. #endif
  366.         }
  367.         switch (header->header.linkflag) {
  368.         case LF_BLK: case LF_CHR:
  369.             st->st_rdev = makedev(from_oct(8, header->header.devmajor),
  370.                       from_oct(8, header->header.devminor));
  371.         }
  372.     } else {
  373.         /* Old fashioned tar archive */
  374.         *stdp = 0;
  375.         st->st_uid = from_oct(8,  header->header.uid);
  376.         st->st_gid = from_oct(8,  header->header.gid);
  377.         st->st_rdev = 0;
  378.     }
  379. }
  380.  
  381.  
  382. /*
  383.  * Quick and dirty octal conversion.
  384.  *
  385.  * Result is -1 if the field is invalid (all blank, or nonoctal).
  386.  */
  387. long
  388. from_oct(digs, where)
  389.     register int    digs;
  390.     register char    *where;
  391. {
  392.     register long    value;
  393.  
  394.     while (isspace(*where)) {               /* Skip spaces */
  395.         where++;
  396.         if (--digs <= 0)
  397.             return -1;        /* All blank field */
  398.     }
  399.     value = 0;
  400.     while (digs > 0 && isodigit(*where)) {  /* Scan til nonoctal */
  401.         value = (value << 3) | (*where++ - '0');
  402.         --digs;
  403.     }
  404.  
  405.     if (digs > 0 && *where && !isspace(*where))
  406.         return -1;            /* Ended on non-space/nul */
  407.  
  408.     return value;
  409. }
  410.  
  411.  
  412. /*
  413.  * Actually print it.
  414.  *
  415.  * Plain and fancy file header block logging.
  416.  * Non-verbose just prints the name, e.g. for "tar t" or "tar x".
  417.  * This should just contain file names, so it can be fed back into tar
  418.  * with xargs or the "-T" option.  The verbose option can give a bunch
  419.  * of info, one line per file.    I doubt anybody tries to parse its
  420.  * format, or if they do, they shouldn't.  Unix tar is pretty random here
  421.  * anyway.
  422.  *
  423.  * Note that print_header uses the globals <head>, <hstat>, and
  424.  * <head_standard>, which must be set up in advance.  This is not very clean
  425.  * and should be cleaned up.  FIXME.
  426.  */
  427. #define UGSWIDTH    11        /* min width of User, group, size */
  428. #define DATEWIDTH    19        /* Last mod date */
  429. static int    ugswidth = UGSWIDTH;    /* Max width encountered so far */
  430.  
  431. void
  432. print_header()
  433. {
  434.     char modes[11];
  435.     char *timestamp;
  436.     char uform[11], gform[11];    /* These hold formatted ints */
  437.     char *user, *group;
  438.     char size[24];        /* Holds a formatted long or maj, min */
  439.     long longie;        /* To make ctime() call portable */
  440.     int    pad;
  441.     char *name;
  442.     extern long baserec;
  443.  
  444.     if(f_sayblock)
  445.         fprintf(msg_file,"rec %10d: ",baserec + (ar_record - ar_block));
  446.     /* annofile(msg_file, (char *)NULL); */
  447.  
  448.     if (f_verbose <= 1) {
  449.         /* Just the fax, mam. */
  450.         char *name;
  451.  
  452.         name=quote_copy_string(head->header.name);
  453.         if(name==0)
  454.             name=head->header.name;
  455.         fprintf(msg_file, "%s\n", name);
  456.         if(name!=head->header.name)
  457.             free(name);
  458.     } else {
  459.         /* File type and modes */
  460.         modes[0] = '?';
  461.         switch (head->header.linkflag) {
  462.         case LF_VOLHDR:
  463.             modes[0]='V';
  464.             break;
  465.  
  466.         case LF_MULTIVOL:
  467.             modes[0]='M';
  468.             break;
  469.  
  470.         case LF_SPARSE:
  471.         case LF_NORMAL:
  472.         case LF_OLDNORMAL:
  473.         case LF_LINK:
  474.                 modes[0] = '-';
  475.                 if ('/' == head->header.name[strlen(head->header.name)-1])
  476.                     modes[0] = 'd';
  477.                 break;
  478.         case LF_DUMPDIR:modes[0] = 'd'; break;
  479.         case LF_DIR:    modes[0] = 'd'; break;
  480.         case LF_SYMLINK:modes[0] = 'l'; break;
  481.         case LF_BLK:    modes[0] = 'b'; break;
  482.         case LF_CHR:    modes[0] = 'c'; break;
  483.         case LF_FIFO:    modes[0] = 'p'; break;
  484.         case LF_CONTIG: modes[0] = 'C'; break;
  485.         }
  486.  
  487.         demode((unsigned)hstat.st_mode, modes+1);
  488.  
  489.         /* Timestamp */
  490.         longie = hstat.st_mtime;
  491.         timestamp = ctime(&longie);
  492.         timestamp[16] = '\0';
  493.         timestamp[24] = '\0';
  494.  
  495.         /* User and group names */
  496.         if (*head->header.uname && head_standard) {
  497.             user  = head->header.uname;
  498.         } else {
  499.             user = uform;
  500.             (void)sprintf(uform, "%d", (int)hstat.st_uid);
  501.         }
  502.         if (*head->header.gname && head_standard) {
  503.             group = head->header.gname;
  504.         } else {
  505.             group = gform;
  506.             (void)sprintf(gform, "%d", (int)hstat.st_gid);
  507.         }
  508.  
  509.         /* Format the file size or major/minor device numbers */
  510.         switch (head->header.linkflag) {
  511.         case LF_CHR:
  512.         case LF_BLK:
  513.             (void)sprintf(size, "%d,%d",
  514.                     major(hstat.st_rdev),
  515.                     minor(hstat.st_rdev));
  516.             break;
  517.         case LF_SPARSE:
  518.             (void)sprintf(size, "%ld",
  519.                 from_oct(1+12, head->header.realsize));
  520.             break;
  521.         default:
  522.             (void)sprintf(size, "%ld", (long)hstat.st_size);
  523.         }
  524.  
  525.         /* Figure out padding and print the whole line. */
  526.         pad = strlen(user) + strlen(group) + strlen(size) + 1;
  527.         if (pad > ugswidth) ugswidth = pad;
  528.  
  529.         name = quote_copy_string(head->header.name);
  530.         if(!name)
  531.             name=head->header.name;
  532.         fprintf(msg_file, "%s %s/%s %*s%s %s %s %.*s",
  533.             modes,
  534.             user,
  535.             group,
  536.             ugswidth - pad,
  537.             "",
  538.             size,
  539.             timestamp+4, timestamp+20,
  540.             sizeof(head->header.name),
  541.             name);
  542.  
  543.         if(name!=head->header.name)
  544.             free(name);
  545.         switch (head->header.linkflag) {
  546.         case LF_SYMLINK:
  547.             name=quote_copy_string(head->header.linkname);
  548.             if(!name)
  549.                 name=head->header.linkname;
  550.             fprintf(msg_file, " -> %s\n", name);
  551.             if(name!=head->header.linkname)
  552.                 free(name);
  553.             break;
  554.  
  555.         case LF_LINK:
  556.             name=quote_copy_string(head->header.linkname);
  557.             if(!name)
  558.                 name=head->header.linkname;
  559.             fprintf(msg_file, " link to %s\n", head->header.linkname);
  560.             if(name!=head->header.linkname)
  561.                 free(name);
  562.             break;
  563.  
  564.         default:
  565.             fprintf(msg_file, " unknown file type '%c'\n",
  566.                 head->header.linkflag);
  567.             break;
  568.  
  569.         case LF_OLDNORMAL:
  570.         case LF_NORMAL:
  571.         case LF_SPARSE:
  572.         case LF_CHR:
  573.         case LF_BLK:
  574.         case LF_DIR:
  575.         case LF_FIFO:
  576.         case LF_CONTIG:
  577.         case LF_DUMPDIR:
  578.             putc('\n', msg_file);
  579.             break;
  580.  
  581.         case LF_VOLHDR:
  582.             fprintf(msg_file, "--Volume Header--\n");
  583.             break;
  584.  
  585.         case LF_MULTIVOL:
  586.             fprintf(msg_file, "--Continued at byte %ld--\n",from_oct(1+12,head->header.offset));
  587.             break;
  588.         }
  589.     }
  590.     fflush(msg_file);
  591. }
  592.  
  593. /*
  594.  * Print a similar line when we make a directory automatically.
  595.  */
  596. void
  597. pr_mkdir(pathname, length, mode)
  598.     char *pathname;
  599.     int length;
  600.     int mode;
  601. {
  602.     char modes[11];
  603.     char *name;
  604.     extern long baserec;
  605.  
  606.     if (f_verbose > 1) {
  607.         /* File type and modes */
  608.         modes[0] = 'd';
  609.         demode((unsigned)mode, modes+1);
  610.  
  611.         if(f_sayblock)
  612.             fprintf(msg_file,"rec %10d: ",baserec + (ar_record - ar_block));
  613.         /* annofile(msg_file, (char *)NULL); */
  614.         name=quote_copy_string(pathname);
  615.         if(!name)
  616.             name=pathname;
  617.         fprintf(msg_file, "%s %*s %.*s\n",
  618.             modes,
  619.             ugswidth+DATEWIDTH,
  620.             "Creating directory:",
  621.             length,
  622.             pathname);
  623.         if(name!=pathname)
  624.             free(name);
  625.     }
  626. }
  627.  
  628.  
  629. /*
  630.  * Skip over <size> bytes of data in records in the archive.
  631.  */
  632. void
  633. skip_file(size)
  634.     register long size;
  635. {
  636.     union record *x;
  637.     extern long save_totsize;
  638.     extern long save_sizeleft;
  639.  
  640.     if(f_multivol) {
  641.         save_totsize=size;
  642.         save_sizeleft=size;
  643.     }
  644.  
  645.     while (size > 0) {
  646.         x = findrec();
  647.         if (x == NULL) {        /* Check it... */
  648.             msg("Unexpected EOF on archive file");
  649.             exit(EX_BADARCH);
  650.         }
  651.         userec(x);
  652.         size -= RECORDSIZE;
  653.         if(f_multivol)
  654.             save_sizeleft-=RECORDSIZE;
  655.     }
  656. }
  657.  
  658. void
  659. skip_extended_headers()
  660. {
  661.     register union record *exhdr;
  662.  
  663.     for (;;) {
  664.         exhdr = findrec();
  665.         if (!exhdr->ext_hdr.isextended) {
  666.         userec(exhdr);
  667.         break;
  668.         }
  669.     }
  670.     userec(exhdr);
  671. }
  672.  
  673. /*
  674.  * Decode the mode string from a stat entry into a 9-char string and a null.
  675.  */
  676. void
  677. demode(mode, string)
  678.     register unsigned mode;
  679.     register char *string;
  680. {
  681.     register unsigned mask;
  682.     register char *rwx = "rwxrwxrwx";
  683.  
  684.     for (mask = 0400; mask != 0; mask >>= 1) {
  685.         if (mode & mask)
  686.             *string++ = *rwx++;
  687.         else {
  688.             *string++ = '-';
  689.             rwx++;
  690.         }
  691.     }
  692.  
  693.     if (mode & S_ISUID)
  694.         if (string[-7] == 'x')
  695.             string[-7] = 's';
  696.         else
  697.             string[-7] = 'S';
  698.     if (mode & S_ISGID)
  699.         if (string[-4] == 'x')
  700.             string[-4] = 's';
  701.         else
  702.             string[-4] = 'S';
  703.     if (mode & S_ISVTX)
  704.         if (string[-1] == 'x')
  705.             string[-1] = 't';
  706.         else
  707.             string[-1] = 'T';
  708.     *string = '\0';
  709. }
  710.